package com.haoshuang.zhouzhoubang.ui.activity


import android.os.Bundle

import androidx.recyclerview.widget.LinearLayoutManager
import com.haoshuang.zhouzhoubang.databinding.ActivityServiceLog2Binding
import com.haoshuang.zhouzhoubang.model.bean.HplBean
import com.haoshuang.zhouzhoubang.ui.adapter.ServiceCaseAdapter2
import com.haoshuang.zhouzhoubang.ui.vm.ServiceLogViewModel2
import com.mufeng.libs.base.BaseVMActivity
import com.mufeng.libs.state.observeState
import com.mufeng.libs.utils.toast
import com.mufeng.libs.utils.view.click


/**
 * 好评率  评价记录
 */
class ServiceLogActivity2 : BaseVMActivity<ServiceLogViewModel2, ActivityServiceLog2Binding>() {

    override fun initView(savedInstanceState: Bundle?) {
        binding.appBar.ivBack.click { finish() }
        binding.appBar.tvTitle.text = "好评率"
    }

    lateinit var mrvServiceAdapter: ServiceCaseAdapter2
    var list = arrayListOf<HplBean>()


    override fun initData() {
        mrvServiceAdapter = ServiceCaseAdapter2(list)
        binding.apply {

            recyclerView.layoutManager = LinearLayoutManager(this@ServiceLogActivity2)
            recyclerView.adapter = mrvServiceAdapter

            refreshLayout.setOnRefreshListener {
                p = 1
                getNet()
                binding.refreshLayout.finishRefresh(2000)
                binding.refreshLayout.setEnableLoadMore(true)
            }
            refreshLayout.setOnLoadMoreListener {
                if (list.size < 10) {
                    refreshLayout.setEnableLoadMore(false)
                } else {
                    p++
                    getNet()
                }
                refreshLayout.finishLoadMore(2000)
            }
        }
        p = 1
        getNet()
    }

    private fun getNet() {
        vm.getData("p" to p)
    }

    override fun startObserve() {
        vm.getData2.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    binding.apply {

                    }
                }
            }
        }
        vm.getData.observeState(this) {
            onLoading { showLoading() }
            onError { code, msg ->
                dismissLoading()
                toast(msg)
            }
            onSuccess {
                dismissLoading()
                if (it != null) {
                    if (p == 1) {
                        list.clear()
                        list.addAll(it)
                        mrvServiceAdapter.notifyDataSetChanged()
                    }
                }
            }
        }
    }

}